{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'d1': 0.21461039158385453,\n",
       "  'd2': 1535706024.622149,\n",
       "  'd3': (120.00289795295261, 43.77763021492626)},\n",
       " {'d1': 0.9309530258204171,\n",
       "  'd2': 1535706201.1172903,\n",
       "  'd3': (113.48368249190275, 47.63413979436062)},\n",
       " {'d1': 0.7838253423300616,\n",
       "  'd2': 1535705880.686862,\n",
       "  'd3': (114.52139680134778, 44.96493267207444)},\n",
       " {'d1': 0.927343855263076,\n",
       "  'd2': 1535705352.9278796,\n",
       "  'd3': (115.10825844318025, 42.38468316013319)},\n",
       " {'d1': 0.8734188110772612,\n",
       "  'd2': 1535705653.058379,\n",
       "  'd3': (124.98590658410461, 41.89441554371871)},\n",
       " {'d1': 0.43029712539758447,\n",
       "  'd2': 1535705977.5383666,\n",
       "  'd3': (107.09464961010116, 35.98913840741729)},\n",
       " {'d1': 0.9740082680121742,\n",
       "  'd2': 1535705306.173345,\n",
       "  'd3': (112.85812894890682, 43.36282845690754)},\n",
       " {'d1': 0.9422915334516604,\n",
       "  'd2': 1535706238.1682642,\n",
       "  'd3': (117.90439682690892, 40.11071039332201)},\n",
       " {'d1': 0.8528216016622017,\n",
       "  'd2': 1535704749.732817,\n",
       "  'd3': (113.8239745126323, 39.66924742250461)},\n",
       " {'d1': 0.5494458746611942,\n",
       "  'd2': 1535704249.2655866,\n",
       "  'd3': (125.884865603778, 49.14033762496654)}]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data\n",
    "import random\n",
    "import time\n",
    "Len=10\n",
    "tagData=[{\"d1\":1,\"d2\":2,\"d3\":3}]\n",
    "\n",
    "dataMapD1=[random.uniform(0.2,0.997) for i in range(Len)]\n",
    "dataMapD2=[random.uniform(time.time()-1000,time.time()+1000) for i in range(Len)]\n",
    "dataMapD3=[(random.uniform(116.403406-10,116.403406+10),random.uniform(39.914935-10,39.914935+10)) for i in range(Len)]\n",
    "tagData=[{\"d1\":i,\"d2\":j,\"d3\":k} for i ,j,k in zip(dataMapD1,dataMapD2,dataMapD3)]\n",
    "with open(\"tagData.csv\",\"w\") as f:\n",
    "    f.wirte(tagData)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1535704314.5582507"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# K means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'numpy' has no attribute 'loadData'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-c97288cb2bd3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcluster\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mKMeans\u001b[0m   \u001b[1;31m#导入sklearn相关包\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcityName\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloadData\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'city.txt'\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#下载样本\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mkm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mKMeans\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn_clusters\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m    \u001b[1;31m#聚类中心为4\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mlabel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit_predict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#得到聚类后的标签\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'loadData'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.cluster import KMeans   #导入sklearn相关包\n",
    "data,cityName =  loadData('city.txt')  #下载样本\n",
    "km = KMeans(n_clusters=4)    #聚类中心为4\n",
    "label = km.fit_predict(data)  #得到聚类后的标签\n",
    "expenses = np.sum(km.cluster_centers_,axis=1)  #按行求和\n",
    "CityCluster = [[],[],[],[]]\n",
    "for i in range(len(cityName)):\n",
    "    CityCluster[label[i]].append(cityName[i])\n",
    "for i in range(len(CityCluster)):\n",
    "    print(\"Expenses:%.2f\" % expenses[i])\n",
    "    print(CityCluster[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
